package com.voxel.sense.infomsg.module.firm.details.video;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.com.common.app.PresenterFragment;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.EmptyView;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.firm.details.FirmDtlInfoListContract;
import com.voxel.sense.infomsg.utils.JZMediaIjkplayer;
import com.voxel.sense.infomsg.view.MyJZVideoPlayerPlay;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;

import butterknife.BindView;
import cn.jzvd.Jzvd;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.BaseEntity;

/**
 * 公司详情视频模块界面
 * Created by chenyiyao on 2017/11/8.
 */

public class FirmVideoFrag extends PresenterFragment<FirmDtlInfoListContract.Presenter> implements FirmDtlInfoListContract.View<BaseEntity> {
    @BindView(R.id.empty_view)
    EmptyView empty_view;
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    VideoResourceAdapter mAdapter;
    private String mCompanyid;


    @Override
    protected FirmDtlInfoListContract.Presenter initPresenter() {
        return new FirmVideoInfoPresenter(this);
    }

    @Override
    protected void initArgs(Bundle bundle) {
        super.initArgs(bundle);
        if (bundle == null)
            return;
        mCompanyid = bundle.getString(IntentConstants.STRING_TYPE);

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.frag_resource_video;
    }

    @Override
    protected void initData() {
        super.initData();
        empty_view.bind(recycle_view);
        empty_view.setRefreshBtnVisible(false);
        mAdapter = new VideoResourceAdapter(new RecyclerAdapter.AdapterListenerImpl() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, Object o) {
                super.onItemClick(holder, o);
            }
        });
        recycle_view.setLayoutManager(new LinearLayoutManager(getActivity()));
        recycle_view.addItemDecoration(new LinearItemDecoration((R.drawable.recycle_divider)));
        recycle_view.setAdapter(mAdapter);

        mPresenter.doLoadData(mCompanyid);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        //监听item的依附和移除
        recycle_view.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
            @Override
            public void onChildViewAttachedToWindow(View view) {

            }

            @Override
            public void onChildViewDetachedFromWindow(View view) {
                MyJZVideoPlayerPlay jzvd = view.findViewById(R.id.video_player);
                //在自定义的播放器中，由于存在handler会延迟播放，这时也需要移除，否则出现播放错乱等问题。
                jzvd.removeHandler();
                if (jzvd != null && Jzvd.CURRENT_JZVD != null
                        && jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
                    if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                        MyJZVideoPlayerPlay.resetAllVideos();
                    }
                }
            }
        });
    }

    @Override
    public RecyclerAdapter getRecyclerViewAdapter() {
        return mAdapter;
    }

    @Override
    public void onAdapterDataChanged() {
        boolean b = mAdapter.getItemCount() > 0;
        if (b) {
            empty_view.triggerOk();
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }

    }

    @Override
    public void showEmptyView(String desc, boolean btnVisible) {
        empty_view.triggerError(desc);
    }

    @Override
    public void showLoading(String str) {
        empty_view.triggerLoading();
    }


}
